package bit2;

import java.util.Random;

public class bitMain20 {
    private static final long count=100000000;
    private static void concurrency(){
        long begin=System.currentTimeMillis();
        Thread thread=new Thread(new Runnable(){
            @Override
            public void run() {
                long a=0;
                for (long i = 0; i < count; i++) {
                    System.out.println("a="+a);
                    a++;
                }
            }
        });
        thread.start();
        Thread thread1=new Thread(new Runnable(){
            @Override
            public void run() {
                long b=0;
                for (long i = 0; i < count; i++) {
                    System.out.println("b="+b);
                    b++;
                }
            }
        });
        thread1.start();
        long end=System.currentTimeMillis();
        System.out.println(end-begin);
    }
    private static void concurrency1(){
        long begin=System.currentTimeMillis();
        Thread thread=new Thread(new Runnable(){
            @Override
            public void run() {
                long a=0;
                for (long i = 0; i < count; i++) {
                    a++;
                }
            }
        });
        thread.start();
        long b=0;
        for (long i = 0; i < count; i++) {
            b++;
        }
        long end=System.currentTimeMillis();
        System.out.println(end-begin);
    }
    private static void serial(){
        long begin=System.currentTimeMillis();
        long b=0;
        for (long i = 0; i < count; i++) {
            b++;
        }
        long a=0;
        for (long i = 0; i < count; i++) {
            a++;
        }
        long end=System.currentTimeMillis();
        System.out.println(end-begin);
    }
    public static void main2(String[] args) {
        concurrency();
    }
    private static class Mythread extends Thread{
        @Override
        public void run() {
            Random random=new Random();
            while (true){
                System.out.println(Thread.currentThread().getName());
                try{
                    Thread.sleep(random.nextInt(10));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main1(String[] args) {
        Mythread t1=new Mythread();
        Mythread t2=new Mythread();
        Mythread t3=new Mythread();
        t1.start();
        t2.start();
        t3.start();
        Random random = new Random();
        while (true) {
            // 打印线程名称
            System.out.println(Thread.currentThread().getName());
            try {
                Thread.sleep(random.nextInt(10));
            } catch (InterruptedException e) {
                // 随机停止运行 0-9 秒
                e.printStackTrace();
            }
        }
    }
    private static class MyRunnable implements Runnable{
        public volatile boolean isQuit=false;
        @Override
        public void run() {
            while (!isQuit){
                System.out.println(Thread.currentThread().getName()+":别管我，我忙着转账呢!");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + ": 啊！险些误了大事");
            }
        }
    }
    public static void main(String[] args) throws InterruptedException {
        MyRunnable target=new MyRunnable();
        Thread thread=new Thread(target,"李四");
        System.out.println(Thread.currentThread().getName() + ": 让李四开始转账。");
        thread.start();
        Thread.sleep(1000);
        System.out.println(Thread.currentThread().getName() + ": 老板来电话了，得赶紧通知李四对方是个骗子！");
        target.isQuit=true;

    }
}
